import {
	mapState,
	mapMutations,
	mapGetters ,
	mapActions  
} from 'vuex'
import store from "./store.js"
// console.log(store._mutations)
var mutations = [];
for(var i in store._mutations){
	mutations.push(i);
}
var actions = [];
for(var i in store._actions){
	actions.push(i);
}
// 尝试将用户在根目录中的store/index.js的vuex的state变量，全部加载到全局变量中
let $uStoreKey = [];
var getss = [];
for(let i in store.getters){
	getss.push(i);
}
try {
	$uStoreKey = store.state ? Object.keys(store.state) : [];
} catch (e) {

}
var MyPlugin = {};
MyPlugin.install = function(Vue, options) {
	// 1. 添加全局方法或属性
	Vue.myGlobalMethod = function() {
		// 逻辑...
	}

	// 2. 添加全局资源
	Vue.directive('my-directive', {
		bind(el, binding, vnode, oldVnode) {
			// 逻辑...
		}
	})

	// 3. 注入组件选项
	Vue.mixin({
		data() {
			return {
				createds: [],
				activated: [],
				mounted: [],
				updated: [],
				destroyed:[]
			}
		},
		runfunc(vue) {
			// 调用周期函数 vue.runfunc(数据类型string/[] c:created m:mounted a:activated u:updated d:destroyed,func/运行代码块)
			// vue.runfunc(['a','c'],function(){
			// 	vue.boxcheck(()=>{
			// 		vue.height =  $('.content').height() - $('.table').position().top + 30
			// 		vue.$refs.singleTable.setCurrentRow(vue.tableData[2]);//默认选择列
			// 	})
			// })
			// vue.runfunc('m',function(){
			// 	vue.boxcheck(()=>{
			// 		console.log(vue.height)
			// 	})
				
			// })
		},
		beforeCreate() {
			// 将vuex方法挂在到$u中
			// 使用方法为：如果要修改vuex的state中的user.name变量为"史诗" => this.$u.vuex('user.name', '史诗')
			// 如果要修改vuex的state的version变量为1.0.1 => this.$u.vuex('version', '1.0.1')
			this.vuex = (name, value, time) => {
				this.$store.commit('$uStore', {
					name,
					value,
					time
				})
			}
			this.setstate = (name,value,cooktname,time)=>{
				this.$store.state[name] = value;
				$cookies.set(cooktname, JSON.stringify(value), time?time:"0");
			}
		},
		created() {
			this.$options.runfunc ? this.$options.runfunc(this) : null;
		},
		activated(){
			if(!$('.tableitem').length) return;
			try{
				// this.height =  $('.wrapper').height() - document.getElementsByClassName('tableitem')[0].getBoundingClientRect().top - 10;
			}catch(e){
				//TODO handle the exception
			}
		},
		mounted(){
		},
		filters:{
			dateymdhms(fmts){
				if (!fmts) return ;
				Date.prototype.format = function (fmt) {
					var o = {
					"M+": this.getMonth() + 1, //月份
					"d+": this.getDate(), //日
					"h+": this.getHours(), //小时
					"m+": this.getMinutes(), //分
					"s+": this.getSeconds(), //秒
					"q+": Math.floor((this.getMonth() + 3) / 3), //季度
					"S": this.getMilliseconds() //毫秒
					};
					if (/(y+)/.test(fmt)) {
					fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
					}
					for (var k in o) {
					if (new RegExp("(" + k + ")").test(fmt)) {
					fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
					}
					}
					return fmt;
				}
				return fmts.format('yyyy/MM/dd hh:mm:ss')
			}
		},
		directives:{
			
		},
		computed: {
			// 将vuex的state中的所有变量，解构到全局混入的mixin中
			meta_title(){
				return this.$route.meta.title?this.$route.meta.title:'未定义'
			},
			...mapState($uStoreKey),
			...mapGetters(getss)
		},
		methods: {
			...mapMutations(mutations),
			...mapActions(actions),
			reloadtab(name){//重载表格
				this.$nextTick(()=>{
					this.$refs[name].doLayout()
				})
			},
			getlouname(id){
				var name = '--';
				for(var i in this.loudonglst){
					let o = this.loudonglst[i];
					if(id == o.id){
						name = o.buildingName;
						break;
					}
				}
				return name;
			},
			getjigouname(id){
				let name = '--';
				function func(arr){
					for(var i in arr){
						let o = arr[i];
						if(id == o.id){
							// console.log(id,o.id,o.companyName)
							name = o.companyName;
							return name;
						}else if(o.child && o.child.length){
							func(o.child)
						}
					}
				}
				func(this.jigoulst);
				// console.log(name)
				return name;
			},
			getcenname(id){
				let name = '--';
				for(var i in this.loucenlst){
					if(id == this.loucenlst[i].id){
						name = this.loucenlst[i].areaName;
						break;
					}
				}
				return name;
			},
			getfangname(id){
				let name = '--';
				for(var i in this.loufanglst){
					// console.log(id,this.loufanglst[i].id)
					if(id == this.loufanglst[i].id){
						name = this.loufanglst[i].areaName;
						break;
					}
				}
				return name;
			},
			getdongname(id){
				let name = '--';
				for(var i in this.loudonglst){
					if(id == this.loudonglst[i].id){
						name = this.loudonglst[i].buildingName;
						break;
					}
				}
				return name;
			},
			dateformat(fmts,type){
				if (!fmts) return ;
				Date.prototype.format = function (fmt) {
					var o = {
					"M+": this.getMonth() + 1, //月份
					"d+": this.getDate(), //日
					"h+": this.getHours(), //小时
					"m+": this.getMinutes(), //分
					"s+": this.getSeconds(), //秒
					"q+": Math.floor((this.getMonth() + 3) / 3), //季度
					"S": this.getMilliseconds() //毫秒
					};
					if (/(y+)/.test(fmt)) {
					fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
					}
					for (var k in o) {
					if (new RegExp("(" + k + ")").test(fmt)) {
					fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
					}
					}
					return fmt;
				}
				return !type?fmts.format('yyyy/MM/dd hh:mm:ss'):fmts.format(type)
			},
			axiosquery(json){//全局设置axios请求参数
				for(var i in json){
					this.axios.defaults.data[i] = json[i];
					this.axios.defaults.params[i] = json[i];
				}
			},
			axiosheader(json){//全局设置axios请求头参数
				for(var i in json){
					this.axios.defaults.headers[i] = json[i];
				}
			},
			runfunc(type,func){
				if(typeof (type) == 'string'){
					if(type == 'c' || !type){
						this.createds.push(func)
					}else if(type == 'm'){
						this.mounted.push(func)
					}else if(type == 'a'){
						this.activated.push(func)
					}else if(type == 'u'){
						this.updated.push(func)
					}else{
						this.destroyed.push(func)
					}
				}else{
					type.forEach((key)=>{
						if(key == 'c'){
							this.createds.push(func)
						}else if(key == 'm'){
							this.mounted.push(func)
						}else if(key == 'a'){
							this.activated.push(func)
						}else if(type == 'u'){
							this.updated.push(func)
						}else{
							this.destroyed.push(func)
						}
					})
				}
				
			},
			boxcheck(func) {
				// console.log(func)
				if (this.$nextTick) {
					this.$nextTick(() => {
						func();
						window.addEventListener('resize', () => {
							func();
						})
					})
				} else {
					func();
					window.addEventListener('resize', () => {
						func();
					})
				}
			}
		}
	})
}
export default MyPlugin;
